/**
 * Copyright &copy; 2012-2014 <a href="https://github.com.jims.base">JeeSite</a> All rights reserved.
 */
package com.jims.master.modules.patient.health.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.patient.health.dao.MedicalHistDao;
import com.jims.master.modules.patient.health.entity.MedicalHist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 家族病史Service
 * @author xueyx
 * @version 2017-07-20
 */
@Service
@Transactional(readOnly = true)
public class MedicalHistService extends CrudService<MedicalHistDao, MedicalHist> {

	@Autowired
	MedicalHistDao medicalHistDao;
	@Autowired
	GeneticDictService geneticDictService;
	@Override
    public Page<MedicalHist> findPage(Page<MedicalHist> page, MedicalHist medicalHist) {
		return super.findPage(page, medicalHist);
	}

	public MedicalHist findByBaseId(String baseid){
		List<MedicalHist> list = medicalHistDao.findByBaseId(baseid);
		if (list!=null && list.size()>0){
			return list.get(0);
		}
		return null;
	}

	// 保存家族其他成员的病史
	@Transactional(readOnly = false)
	public void saveAll(MedicalHist medicalHist) {
		// 首先根据user_id 取出原有的hist， 新值覆盖
		String baseid = medicalHist.getBaseInfo();
		MedicalHist medicalHist1 = new MedicalHist();
		List<MedicalHist> list = medicalHistDao.findByBaseId(baseid);
		if (list!=null && list.size()>0){
			medicalHist1 =  list.get(0);
		}else {
			medicalHist1.setBaseInfo(baseid);
		}

		// 遗传病权值更新

		geneticDictService.update(medicalHist1.getFather(), medicalHist.getFather());
		geneticDictService.update(medicalHist1.getMother(), medicalHist.getMother());
		geneticDictService.update(medicalHist1.getBrothers(), medicalHist.getBrothers());
		geneticDictService.update(medicalHist1.getChildren(), medicalHist.getChildren());

		// 用新提交的值覆盖掉原有值
		medicalHist1.setId(medicalHist.getId());
		medicalHist1.setFather(medicalHist.getFather());
		medicalHist1.setFaOther(medicalHist.getFaOther());
		medicalHist1.setMother(medicalHist.getMother());
		medicalHist1.setMaOther(medicalHist.getMaOther());
		medicalHist1.setBrothers(medicalHist.getBrothers());
		medicalHist1.setBrOther(medicalHist.getBrOther());
		medicalHist1.setChildren(medicalHist.getChildren());
		medicalHist1.setChOther(medicalHist.getChOther());
		medicalHist1.setFaIdcard(medicalHist.getFaIdcard());
		medicalHist1.setMaIdcard(medicalHist.getMaIdcard());
		medicalHist1.setBrIdcard(medicalHist.getBrIdcard());
		medicalHist1.setChIdcard(medicalHist.getChIdcard());
		medicalHist1.setUpdateDate(medicalHist.getUpdateDate());
		medicalHist1.setUpdateBy(medicalHist.getUpdateBy());
		if(StringUtils.hasLength(medicalHist1.getBaseInfo())){
			if(list!=null && list.size()>0){
				medicalHistDao.update(medicalHist1);
			}else{
				medicalHistDao.insert(medicalHist1);
			}
		}
	}


	// 保存自己的病史
	@Transactional(readOnly = false)
	public void saveSelf(MedicalHist medicalHist) {
		String baseid = medicalHist.getBaseInfo();
		MedicalHist medicalHist1 = new MedicalHist();
		List<MedicalHist> list = medicalHistDao.findByBaseId(baseid);
		if (list!=null && list.size()>0){
			medicalHist1 =  list.get(0);
		}else {
			medicalHist1.setBaseInfo(baseid);
			medicalHistDao.insert(medicalHist1);
		}
		geneticDictService.update(medicalHist1.getSelf(), medicalHist.getSelf());
		medicalHist1.setSelf(medicalHist.getSelf());
		medicalHist1.setSeOther(medicalHist.getSeOther());
		medicalHistDao.update(medicalHist1);

	}
  public MedicalHist findByIdcard(String brIdcard){
	  return medicalHistDao.findByIdcard(brIdcard);
  }
}